#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STUDENTS 100

// 定义体检信息结构体
struct Checkup {
	int student_id;
	char name[50];
	char gender[10];
	char department[50];
	int age;
	float height;
	float weight;
	float chest_size;
	char date[20];
};

// 定义病历信息结构体
struct MedicalRecord {
	int student_id;
	char name[50];
	char gender[10];
	char department[50];
	char diagnosis[100];
	char date[20];
};

// 学生健康档案
struct HealthRecord {
	struct Checkup checkup;
	struct MedicalRecord medical_record;
};

// 学生健康档案数据库
struct HealthRecord health_records[MAX_STUDENTS];
int num_records = 0;

// 函数声明
void registerRecord();
void modifyRecord();
void deleteRecord();
void queryRecord();
void statistics();
void printCheckup(struct Checkup record);
void printMedicalRecord(struct MedicalRecord record);
float calculateAverageHeight();
float calculateAverageWeight();
float calculateAverageAge();
float calculateAverageChestSize();
float calculateAverageGrowthRate();
float calculateAverageWeightGainRate();

int main() {
	int choice;
	
	do {
		system("CLS");
		printf("\n\t\t  学生健康档案管理系统");
		printf("\n\t\t========================");
		printf("\n\t\t\t 1.登记");
		printf("\n\t\t\t 2.修改");
		printf("\n\t\t\t 3.删除");
		printf("\n\t\t\t 4.查询");
		printf("\n\t\t\t 5.统计");
		printf("\n\t\t\t 0.退出");
		printf("\n\t\t========================");
		printf("\n\t\t  请输入选项：");
		scanf("%d", &choice);
		system("cls");
		
		switch (choice) {
		case 1:
			registerRecord();
			break;
		case 2:
			modifyRecord();
			break;
		case 3:
			deleteRecord();
			break;
		case 4:
			queryRecord();
			break;
		case 5:
			statistics();
			break;
		case 0:
			printf("谢谢使用！\n");
			break;
		default:
			printf("无效选项，请重新输入。\n");
		}
	} while (choice != 0);
	
	system("pause");
	
	return 0;
}

void registerRecord() {
	struct HealthRecord new_record;
	
	printf("请输入学号: ");
	scanf("%d", &new_record.checkup.student_id);
	printf("请输入姓名: ");
	scanf("%s", new_record.checkup.name);
	printf("请输入性别: ");
	scanf("%s", new_record.checkup.gender);
	printf("请输入系别: ");
	scanf("%s", new_record.checkup.department);
	printf("请输入年龄: ");
	scanf("%d", &new_record.checkup.age);
	printf("请输入身高: ");
	scanf("%f", &new_record.checkup.height);
	printf("请输入体重: ");
	scanf("%f", &new_record.checkup.weight);
	printf("请输入胸围: ");
	scanf("%f", &new_record.checkup.chest_size);
	printf("请输入体检日期: ");
	scanf("%s", new_record.checkup.date);
	
	printf("请输入诊断信息: ");
	scanf("%s", new_record.medical_record.diagnosis);
	printf("请输入病历日期: ");
	scanf("%s", new_record.medical_record.date);
	
	health_records[num_records++] = new_record;
	
	printf("登记成功！\n");
	
	system("pause");
}

void modifyRecord() {
	int id, found = 0;
	
	printf("请输入要修改的学生学号: ");
	scanf("%d", &id);
	
	for (int i = 0; i < num_records; ++i) {
		if (health_records[i].checkup.student_id == id) {
			printf("请输入修改后的姓名: ");
			scanf("%s", health_records[i].checkup.name);
			printf("请输入修改后的性别: ");
			scanf("%s", health_records[i].checkup.gender);
			printf("请输入修改后的系别: ");
			scanf("%s", health_records[i].checkup.department);
			printf("请输入修改后的年龄: ");
			scanf("%d", &health_records[i].checkup.age);
			printf("请输入修改后的身高: ");
			scanf("%f", &health_records[i].checkup.height);
			printf("请输入修改后的体重: ");
			scanf("%f", &health_records[i].checkup.weight);
			printf("请输入修改后的胸围: ");
			scanf("%f", &health_records[i].checkup.chest_size);
			printf("请输入修改后的体检日期: ");
			scanf("%s", health_records[i].checkup.date);
			
			printf("请输入修改后的诊断信息: ");
			scanf("%s", health_records[i].medical_record.diagnosis);
			printf("请输入修改后的病历日期: ");
			scanf("%s", health_records[i].medical_record.date);
			
			found = 1;
			
			printf("修改成功！\n");
			
			system("pause");
			break;
		}
	}
	
	if (!found) {
		printf("未找到该学生的记录！\n");
	}
}

void deleteRecord() {
	int id, found = 0;
	
	printf("请输入要删除的学生学号: ");
	scanf("%d", &id);
	
	for (int i = 0; i < num_records; ++i) {
		if (health_records[i].checkup.student_id == id) {
			for (int j = i; j < num_records - 1; ++j) {
				health_records[j] = health_records[j + 1];
			}
			num_records--;
			found = 1;
			
			printf("删除成功！\n");
			
			system("pause");
			break;
		}
	}
	
	if (!found) {
		printf("未找到该学生的记录！\n");
	}
}

void queryRecord() {
	int id, found = 0;
	
	printf("请输入要查询的学生学号: ");
	scanf("%d", &id);
	
	for (int i = 0; i < num_records; ++i) {
		if (health_records[i].checkup.student_id == id) {
			printf("=== 体检信息 ===\n");
			printCheckup(health_records[i].checkup);
			printf("=== 病历信息 ===\n");
			printMedicalRecord(health_records[i].medical_record);
			found = 1;
			system("pause");
			break;
		}
	}
	
	if (!found) {
		printf("未找到该学生的记录！\n");
	}
}


// 统计功能
void statistics() {
	printf("=== 基本健康状况统计 ===\n");
	printf("1. 平均身高: %.2f\n", calculateAverageHeight());
	printf("2. 平均体重: %.2f\n", calculateAverageWeight());
	printf("3. 平均年龄: %.2f\n", calculateAverageAge());
	printf("4. 平均胸围: %.2f\n", calculateAverageChestSize());
	
	float average_growth_rate = calculateAverageGrowthRate();
	if (average_growth_rate != -1) {
		printf("5. 平均年增长值: %.2f\n", average_growth_rate);
	}
	else {
		printf("5. 平均年增长值: 无法计算\n");
	}
	
	float average_weight_gain_rate = calculateAverageWeightGainRate();
	if (average_weight_gain_rate != -1) {
		printf("6. 平均年增重率: %.2f%%\n", average_weight_gain_rate);
	}
	else {
		printf("6. 平均年增重率: 无法计算\n");
	}
}


void printCheckup(struct Checkup record) {
	printf("学号: %d\n", record.student_id);
	printf("姓名: %s\n", record.name);
	printf("性别: %s\n", record.gender);
	printf("系别: %s\n", record.department);
	printf("年龄: %d\n", record.age);
	printf("身高: %.2f\n", record.height);
	printf("体重: %.2f\n", record.weight);
	printf("胸围: %.2f\n", record.chest_size);
	printf("体检日期: %s\n", record.date);
}

void printMedicalRecord(struct MedicalRecord record) {
	printf("学号: %d\n", record.student_id);
	printf("姓名: %s\n", record.name);
	printf("性别: %s\n", record.gender);
	printf("系别: %s\n", record.department);
	printf("诊断信息: %s\n", record.diagnosis);
	printf("病历日期: %s\n", record.date);
}

float calculateAverageHeight() {
	float total_height = 0;
	for (int i = 0; i < num_records; ++i) {
		total_height += health_records[i].checkup.height;
	}
	return total_height / num_records;
}

float calculateAverageWeight() {
	float total_weight = 0;
	for (int i = 0; i < num_records; ++i) {
		total_weight += health_records[i].checkup.weight;
	}
	return total_weight / num_records;
}

float calculateAverageAge() {
	float total_age = 0;
	for (int i = 0; i < num_records; ++i) {
		total_age += health_records[i].checkup.age;
	}
	return total_age / num_records;
}

float calculateAverageChestSize() {
	float total_chest_size = 0;
	for (int i = 0; i < num_records; ++i) {
		total_chest_size += health_records[i].checkup.chest_size;
	}
	return total_chest_size / num_records;
}

float calculateAverageGrowthRate() {
	if (num_records <= 1) {
		return -1; // 无法计算
	}
	
	float total_growth = 0;
	for (int i = 1; i < num_records; ++i) {
		total_growth += health_records[i].checkup.height - health_records[i - 1].checkup.height;
	}
	return total_growth / (num_records - 1);
}

float calculateAverageWeightGainRate() {
	if (num_records <= 1) {
		return -1; // 无法计算
	}
	
	float total_weight_gain = 0;
	for (int i = 1; i < num_records; ++i) {
		total_weight_gain += health_records[i].checkup.weight - health_records[i - 1].checkup.weight;
	}
	return (total_weight_gain / (num_records - 1)) * 100 / calculateAverageWeight();
}
